import Apis from '../../../apis/cowork';
import {message,Modal} from 'antd';
import CoworkAuth from './CoworkAuth';
import {inject, observer} from "mobx-react";
import {toJS} from 'mobx';
import {WeaLocaleProvider} from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;

@inject("coworkMainStore")
@observer
class CoworkTypeUser extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      list: [],
      loading: true
    };
  }
  componentDidMount() {
    this.reload();
  }
  componentWillReceiveProps(nextProps) {
    if (nextProps.coTypeId != this.props.coTypeId) {
      this.reload(nextProps);
    }
  }
  render() {
    let {list, loading} = this.state, {type,coworkMainStore} = this.props;
    let config = toJS(coworkMainStore.config), {authConditions = []} = config;
    return (
      <CoworkAuth id={this.props.id} type={this.props.type} ref="auth" list={list} title={type=='members' ? getLabel(382643, '创建人') : getLabel(2097,'负责人')}
                  authConditions={authConditions} loading={loading}
                  submit={this.submit} delete={this.delete}/>
    );
  }
  submit = (params,func)=> {
    Apis.coworkTypeShareManagerOperation(params).then((result) => {
      if (result.flag) {
        func();
        this.reload();
      } else {
        message.error(getLabel(22620, '保存失败'));
      }
    });
  }
  reload = (props = this.props)=> {
    let {coTypeId, type} = props;
    this.setState({loading: true});
    Apis.getCoworkTypeShareConditionStrList({coTypeId, type}).then((result) => {
      if (result.status == 1) {
        this.setState({
          list: result.alist,
          loading: false
        });
      } else {
        message.error(getLabel(191, '读取协作创建人/负责人失败','error'));
      }
    });
  }
  
  delete = (rows,callback) => {
    if (rows.length == 0) {
      message.info(getLabel(4908, '请选择要删除的内容','note'));
      return;
    }
    let params = {id: rows.map((row) => row.shareid).join(), setType: this.props.type, coTypeId: this.props.id};
    let that = this;
    Modal.confirm({
      content: getLabel(23271, '确认要删除吗') + '?',
      onOk() {
        Apis.coworkTypeShareManagerOperation(params).then((result) => {
          if (result.status == 1) {
            that.reload();
            callback();
          } else {
            message.error(getLabel(20462, '删除失败'));
          }
        });
      }
    })
  }
}

export default CoworkTypeUser;